Skip to content

Shorten key ids in exception messages, check decoded bytes in the leak test, fix a README parameter name - #39

Merged
spaze merged 3 commits into
mainfrom
spaze/hardening
Aug 1, 2026
Merged

Shorten key ids in exception messages, check decoded bytes in the leak test, fix a README parameter name#39
spaze merged 3 commits into
mainfrom
spaze/hardening

Conversation

@spaze

@spaze spaze commented Aug 1, 2026

Copy link
Copy Markdown
Owner

Hardening leftovers from the reviews of #38, none of which belonged in that PR:

  • Key ids shortened in exception messages - a key pasted into an id slot in the configuration ends up repeated in constructor exception messages; the same shortening that already keeps markers and stored key ids out of logs now applies there too, so such a paste leaks at most the first 20 characters instead of the whole key. Ids of sane length are unaffected, every existing message stays the same.
  • Symmetric print_r() leak test checks the decoded bytes - the object stores only decoded raw bytes, never the prefix_hex config strings, so the old needles could not fail even with the HiddenString wrapping removed. The public-key class tests already check both forms; the symmetric test now does the same.
  • README - the runtime-call example read %encryption.keyPrefixes.email% but the parameters section defines the group as prefixes; copying the two snippets together would fail with an undefined parameter when the container compiles.

spaze added 3 commits August 1, 2026 02:52
… key ids already are

A key pasted into an id slot in the configuration is a real mistake this library already defends against elsewhere: the same shortening that keeps markers and key ids from stored values out of logs now also applies to the constructor exceptions that repeat a configured key id, so such a paste leaks at most the first 20 characters instead of the whole key. Ids of sane length are unaffected, every existing message stays the same.

The comment on `LogSafeValue` now names both sources of hostile values, stored data and misconfigured slots.
…ot just the config strings

The object never stores the `prefix_hex` config strings, only the decoded raw bytes wrapped in `HiddenString`, so checking the config strings alone can't fail even if the `HiddenString` wrapping is removed and the raw keys become dumpable. The tests of the two public-key classes already check both forms; the symmetric test now does the same.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the encryption library against accidental key leakage by ensuring long/unsafe key IDs are truncated in constructor-time exception messages, improves the symmetric print_r() leak test to validate against stored decoded key bytes, and fixes a README parameter name mismatch for Nette DI usage.

Changes:

  • Apply LogSafeValue::from() to key-id-like values in several constructor exceptions so pasted keys don’t get repeated in full.
  • Strengthen SymmetricKeyEncryption’s “hidden string” test to assert both the original config string and the decoded raw bytes are not present in print_r() output.
  • Fix README example to use %encryption.prefixes.*% consistently with the parameters section.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/SymmetricKeyEncryptionTest.phpt Updates leak test to check decoded key bytes and adds coverage for key-id truncation in constructor exceptions.
tests/AuthenticatedPublicKeyEncryptionTest.phpt Adds a regression test ensuring long “key pasted as id” values are truncated in constructor exceptions.
tests/AnonymousPublicKeyEncryptionTest.phpt Adds a regression test ensuring long “key pasted as id” values are truncated in key-pair mismatch exceptions.
src/Format/LogSafeValue.php Clarifies docblock to include misconfigured key IDs as inputs that must be shortened/sanitized for exception messages.
src/Exceptions/KeyPairMismatchException.php Sanitizes/truncates key id shown in mismatch message via LogSafeValue.
src/Exceptions/InvalidKeyRoleException.php Sanitizes/truncates the id included in invalid-role messages via LogSafeValue.
src/Exceptions/InvalidKeyPrefixException.php Sanitizes/truncates the id included in invalid/missing prefix messages via LogSafeValue.
src/Exceptions/InvalidKeyLengthException.php Sanitizes/truncates the id included in invalid-length messages via LogSafeValue.
src/Exceptions/InvalidKeyIdException.php Sanitizes/truncates non-empty ids included in invalid-id messages via LogSafeValue.
src/Exceptions/InvalidKeyEncodingException.php Sanitizes/truncates the id included in invalid-encoding messages via LogSafeValue.
src/Exceptions/IncompleteKeyPairException.php Sanitizes/truncates key id shown in incomplete pair messages via LogSafeValue.
README.md Fixes service example to reference %encryption.prefixes.email% consistently with the parameters section.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@spaze
spaze merged commit ce5961b into main Aug 1, 2026
17 checks passed
@spaze
spaze deleted the spaze/hardening branch August 1, 2026 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants